import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/convenient/order/order_pending_page.dart';
import 'package:kkhousekeep/page/home/convenient/order/order_processing_list_controller.dart';
import 'package:kkhousekeep/page/home/convenient/order/order_processing_report_page.dart';
import 'package:kkhousekeep/page/home/convenient/order/repairs_detail_page.dart';
import 'package:kkhousekeep/widget/custom_app_bar.dart';
import 'package:kkhousekeep/widget/custom_order_filter.dart';
import 'package:kkhousekeep/widget/work_order_item.dart';

import '../../../../config/constant.dart';
import '../../../../tools/dialog_util.dart';
import 'order_transfer_page.dart';

class OrderProcessingListPage extends StatelessWidget {
  bool isFromMyWorkOrder = false;

  OrderProcessingListPage({Key? key, required this.isFromMyWorkOrder})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: OrderProcessingListController(isFromMyWorkOrder),
      builder: (controller) => Scaffold(
        appBar: CustomAppBar(
          "处理中",
          actions: [
            Builder(
              builder: (context) => InkWell(
                onTap: () => _showDropdownDialog(context),
                child: SizedBox(
                  width: 48.w,
                  height: 48.h,
                  child: Obx(
                    () => Image.asset(
                      controller.isShowDialog.value
                          ? 'assets/images/home_convenient_order_check.png'
                          : 'assets/images/home_convenient_order_uncheck.png',
                    ),
                  ),
                ).marginOnly(right: 23.w),
              ),
            )
          ],
        ),
        body: Obx(
          () => controller.orderList.isEmpty
              ? _builderEmptyWidget()
              : _builderBody(controller),
        ),
      ),
    );
  }

  Widget _builderEmptyWidget() {
    return Container(
      margin: EdgeInsets.only(top: 292.h),
      alignment: Alignment.center,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Image.asset(
            "assets/images/home_convenient_repair_empty.png",
            width: 288.w,
            height: 370.h,
          ),
          Padding(
            padding: EdgeInsets.only(top: 8.h),
            child: Text(
              "没有指派给你的报修工单",
              style: TextStyle(
                fontSize: 28.sp,
                color: const Color(0XFF999999),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _builderBody(OrderProcessingListController controller) {
    var dataList = controller.orderList;
    return ListView.builder(
      itemCount: dataList.length,
      itemBuilder: (context, index) {
        var model = dataList[index];
        // var isUrgent = index == 0;
        var marginTop = index == 0 ? 32 : 8;
        var marginBottom = index == dataList.length - 1 ? 40 : 8;
        var urgency = "";
        switch (model.urgency) {
          case 1:
            urgency = "一般";
            break;
          case 2:
            urgency = "紧急";
            break;
          case 3:
            urgency = "非常急";
            break;
        }
        return InkWell(
          onTap: () {
            Get.to(const RepairsDetailPage(), arguments: model);
          },
          child: WorkOrderItem(
            time: model.cTime,
            address: model.address,
            urgent: urgency,
            repairType: model.fatalType,
            marginTop: marginTop.toDouble(),
            marginBottom: marginBottom.toDouble(),
            actions: isFromMyWorkOrder
                ? ([
                    WorkOrderItem.buildPrimaryButton(
                      text: "完工",
                      onTap: () => {
                        Get.to(() => const OrderProcessingReportPage(),
                                arguments: model)
                            ?.then(
                          (value) {
                            if (value == Constant.resultOK) {
                              controller.loadList();
                              DialogUtil.showNetworkSucceed();
                            }
                          },
                        )
                      },
                    ),
                    WorkOrderItem.buildButton(
                      text: "转单",
                      onTap: () => {
                        Get.to(() => const OrderTransferPage(),
                                arguments: model)
                            ?.then(
                          (value) {
                            if (value == Constant.resultOK) {
                              DialogUtil.showNetworkSucceed();
                              controller.loadList();
                            }
                          },
                        ),
                      },
                    ),
                    WorkOrderItem.buildButton(
                      text: "挂单",
                      onTap: () => {
                        Get.to(() => const OrderPendingPage(), arguments: model)
                            ?.then(
                          (value) {
                            if (value == Constant.resultOK) {
                              DialogUtil.showNetworkSucceed();
                              controller.loadList();
                            }
                          },
                        )
                      },
                    ),
                  ])
                : null,
          ),
        );
      },
    );
  }

  void _showDropdownDialog(BuildContext context) {
    OrderProcessingListController controller = Get.find();
    controller.isShowDialog.value = true;
    SmartDialog.showAttach(
      targetContext: context,
      alignment: Alignment.bottomCenter,
      highlightBuilder: (Offset targetOffset, Size targetSize) {
        return Positioned(
          child: Container(
            height: targetOffset.dy + targetSize.height + 1.h,
            width: targetOffset.dx + targetSize.width,
            color: Colors.white,
          ),
        );
      },
      builder: (_) => _builderDropdownWidget(),
      onDismiss: () => {controller.isShowDialog.value = false},
    );
  }

  Widget _builderDropdownWidget() {
    OrderProcessingListController controller = Get.find();
    CustomOrderFilter? custom;
    custom = CustomOrderFilter(
      repairId: controller.repairId.value,
      urgentId: controller.urgentId.value,
      onTapConfirm: () {
        if (custom != null) {
          controller.filterList(custom.repairId, custom.urgentId);
        }
        SmartDialog.dismiss();
      },
      onTapReset: () {
        if (custom != null) {
          controller.filterList(0, 0);
        }
        SmartDialog.dismiss();
      },
    );
    return custom;
  }
}
